﻿using System;
using System.Management;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

//senses current running processes, but would be more useful to simply list active applications ala task manager
namespace Sensors
{
    class ProcessSensor
    {
        Process[] proc;

        //Repopulates proc array everytime a new process is created
        public void run2()
        {
            proc = System.Diagnostics.Process.GetProcesses(Environment.MachineName);
            foreach (Process p in proc)
                Console.WriteLine(p.ProcessName);

            ManagementEventWatcher w = null;
            ManagementEventWatcher x = null;
            WqlEventQuery q;
            WqlEventQuery r;

            ProcessSensor psense = new ProcessSensor();
            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;
            try
            {
                q = new WqlEventQuery();
                q.EventClassName = "__InstanceCreationEvent";
                q.WithinInterval = new TimeSpan(0, 0, 10);
                q.Condition = @"TargetInstance ISA 'Win32_Process'";
                Console.WriteLine(q.QueryString);

                r = new WqlEventQuery();
                r.EventClassName = "__InstanceDeletionEvent";
                r.WithinInterval = new TimeSpan(0, 0, 10);
                r.Condition = @"TargetInstance ISA 'Win32_Process'";
                Console.WriteLine(r.QueryString);

                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += new EventArrivedEventHandler(psense.EventArrived2);
                w.Start();

                x = new ManagementEventWatcher(scope, r);
                x.EventArrived += new EventArrivedEventHandler(psense.EventArrived2);
                x.Start();

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                w.Stop();
                x.Stop();
            }
        }
        public void EventArrived2(object sender, EventArrivedEventArgs e)
        {
            proc = System.Diagnostics.Process.GetProcesses();
            foreach (Process p in proc)
                Console.WriteLine(p.ProcessName);
        }


        /*
        public void run()
        {
            try
            {
                ManagementObjectSearcher searcher =
                      new ManagementObjectSearcher("root\\CIMV2",
                      "SELECT * FROM Win32_Process");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Process instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine(queryObj["Caption"]);
                    ManagementObject mbo = new ManagementObject(queryObj["Dependent"].ToString());
//                    foreach (PropertyData prop in mbo.Properties)
//                    {
//                        if (prop.Value != null)
//                            Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
//                    }
                    Console.WriteLine("-----------------------------------");
                }
            }
            catch (ManagementException e)
            {
                Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
            }

            ProcessSensor psense = new ProcessSensor();
            ManagementEventWatcher w = null;
            ManagementEventWatcher x = null;
            WqlEventQuery q;
            WqlEventQuery r;
            ManagementOperationObserver observer = new ManagementOperationObserver();

            ManagementScope scope = new ManagementScope("root\\CIMV2");

            scope.Options.EnablePrivileges = true;

            try
            {
                q = new WqlEventQuery();
                q.EventClassName = "__InstanceCreationEvent";
                q.WithinInterval = new TimeSpan(0, 0, 10);
                q.Condition = @"TargetInstance ISA 'Win32_Process'";
                Console.WriteLine(q.QueryString);

                r = new WqlEventQuery();
                r.EventClassName = "__InstanceDeletionEvent";
                r.WithinInterval = new TimeSpan(0, 0, 10);
                r.Condition = @"TargetInstance ISA 'Win32_Process'";
                Console.WriteLine(r.QueryString);

                w = new ManagementEventWatcher(scope, q);
                w.EventArrived += new EventArrivedEventHandler(psense.EventArrived);
                w.Start();

                x = new ManagementEventWatcher(scope, r);
                x.EventArrived += new EventArrivedEventHandler(psense.EventArrived);
                x.Start();

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                w.Stop();
            }
        }
        */

        /*
        public void EventArrived(object sender, EventArrivedEventArgs e)
        {
            Console.WriteLine("Event Sensed");
            foreach (PropertyData pd in e.NewEvent.Properties)
            {
                Console.WriteLine("{0},{1},{2},{3}", pd.Name, pd.Type, pd.Value, pd.Origin);

                ManagementBaseObject mbo = null;

                if ((mbo = pd.Value as ManagementBaseObject) != null)
                {
                    //Console.WriteLine("---Properties");
                    foreach (PropertyData prop in mbo.Properties)
                    {

                        if (prop.Name.Equals("Caption"))
                            Console.WriteLine("Process Name: {0}", prop.Value);

                        //Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
                    }
                }

            }
            Console.WriteLine("\n");
        }
        */

    }

}
